package com.been.common.action;

import java.util.List;

import com.been.common.exception.ActionHandleException;
import com.been.common.model.BaseModel;
import com.been.common.page.PageInfo;
import com.been.common.service.GenericService;

import static com.been.common.util.StringUtil.isNull;

import static com.been.common.constants.StringContants.EMPTY_STRING;

/**
 * 通用的Action
 * 
 * @author been 2011-10-02
 */
public class GenericAction<S extends GenericService<M>, M extends BaseModel>
		extends PublicAction {

	private static final long serialVersionUID = 1L;

	private ActionHandleInfo actionHandleInfo;

	private S genericService;

	private M baseModel;

	private String pageName;

	private String baseModelName;

	private String deleteIds;

	private String baseModelListName;

	private String queryString;

	private Object[] parameters;

	public GenericAction() {
		actionHandleInfo = new ActionHandleInfo();
	}

	/**
	 * 保存当前action所设置的model
	 * 
	 * @return String 转向和重定向标识
	 */
	public String save() {
		try {
			if (isNull(this.baseModel.getId())) {
				this.genericService.save(baseModel);
				this.actionHandleInfo.setAction("add");
			} else {
				this.genericService.update(baseModel);
				this.actionHandleInfo.setAction("edit");
			}
			this.actionHandleInfo.setCurrentRowId(this.baseModel.getId());
			this.actionHandleInfo.setDeleteRowNums(0);
			return SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			throw new ActionHandleException(pageName, "GenericAction#save: "
					+ e.getMessage());
		}
	}

	/**
	 * 根据ID删除对象。IDS 为 ","拼接的字符串。<br>
	 * 如果1个删除失败。则整体删除失败。
	 * 
	 * @return String 转向和重定向标识
	 */
	public String deleteByIds() {
		try {
			int deleteIndex = this.genericService.deleteByIds(deleteIds);
			this.actionHandleInfo.setAction("remove");
			this.actionHandleInfo.setCurrentRowId(EMPTY_STRING);
			this.actionHandleInfo.setDeleteRowNums(deleteIndex);
			return SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			throw new ActionHandleException(pageName,
					"GenericAction#deleteByIds: " + e.getMessage());
		}
	}

	/**
	 * 删除当前事务管理的BASEMODEL
	 * 
	 * @return String 转向和重定向标识
	 */
	public String delete() {
		try {
			this.genericService.delete(baseModel);
			this.actionHandleInfo.setAction("remove");
			this.actionHandleInfo.setCurrentRowId(EMPTY_STRING);
			this.actionHandleInfo.setDeleteRowNums(1);
			return SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			throw new ActionHandleException(pageName, "GenericAction#delete: "
					+ e.getMessage());
		}
	}

	/**
	 * 根据ID 查询对象
	 * 
	 * @param id
	 *            序列化ID
	 * @return String 转向和重定向标识
	 */
	public String find(String id) {
		try {
			this.getRequest().setAttribute(baseModelName,
					this.genericService.findById(id));
			return SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			throw new ActionHandleException(pageName, "GenericAction#find: "
					+ e.getMessage());
		}
	}

	/**
	 * 查询列表对象。
	 * 
	 * @return String 转向和重定向标识
	 */
	public String list() {
		try {
			PageInfo pageInfo = this.actionHandleInfo.getPageInfo();
			List<M> datalist = this.genericService.findByPage(queryString,
					parameters, pageInfo);
			this.actionHandleInfo.setCurrentRowId(EMPTY_STRING);
			this.actionHandleInfo.setDeleteRowNums(1);
			this.getRequest().setAttribute(this.baseModelListName, datalist);
			return SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			throw new ActionHandleException(pageName, "GenericAction#list: "
					+ e.getMessage());
		}
	}
	
	

	public ActionHandleInfo getActionHandleInfo() {
		return actionHandleInfo;
	}

	public void setActionHandleInfo(ActionHandleInfo actionHandleInfo) {
		this.actionHandleInfo = actionHandleInfo;
	}

	public S getGenericService() {
		return genericService;
	}

	public void setGenericService(S genericService) {
		this.genericService = genericService;
	}

	public M getBaseModel() {
		return baseModel;
	}

	public void setBaseModel(M baseModel) {
		this.baseModel = baseModel;
		this.baseModelName = this.baseModel.getClass().getSimpleName();
	}

	public String getPageName() {
		return pageName;
	}

	public void setPageName(String pageName) {
		this.pageName = pageName;
	}

	public String getBaseModelName() {
		return baseModelName;
	}

	public void setBaseModelName(String baseModelName) {
		this.baseModelName = baseModelName;
	}

	public String getDeleteIds() {
		return deleteIds;
	}

	public void setDeleteIds(String deleteIds) {
		this.deleteIds = deleteIds;
	}

	public String getBaseModelListName() {
		return baseModelListName;
	}

	public void setBaseModelListName(String baseModelListName) {
		this.baseModelListName = baseModelListName;
	}

	public String getQueryString() {
		return queryString;
	}

	public void setQueryString(String queryString) {
		this.queryString = queryString;
	}

	public Object[] getParameters() {
		return parameters;
	}

	public void setParameters(Object[] parameters) {
		this.parameters = parameters;
	}

}
